package models

import (
	"github.com/beego/beego/v2/client/orm"
	"ltbz/cultural_tourism_api/utils"
	"time"
)

type ScenicSpot struct {
	ScenicSpotId   int       `orm:"column(scenic_spot_id);pk"`
	ScenicSpotName string    `description:"景区名称"`
	Address        string    `description:"地址"`
	Status         string    `description:"状态:启用,禁用"`
	CreateTime     time.Time `description:"创建时间"`
	ModifyTime     time.Time `description:"修改时间"`
}

type ScenicSpotView struct {
	ScenicSpotId   int    `description:"景区id"`
	ScenicSpotName string `description:"景区名称"`
	Address        string `description:"地址"`
	Status         string `description:"状态:启用,禁用"`
	CreateTime     string `description:"创建时间"`
	ModifyTime     string `description:"修改时间"`
}

// 新增
func (r *ScenicSpot) Add() (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(r)
	return
}

func (r *ScenicSpot) GetScenicSpotById(scenicSpotId int) (item *ScenicSpotView, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM scenic_spot WHERE scenic_spot_id=? `
	err = o.Raw(sql, scenicSpotId).QueryRow(&item)
	return
}

func (r *ScenicSpot) GetScenicSpotPageCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM scenic_spot WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (r *ScenicSpot) GetScenicSpotPage(condition string, pars []interface{}, startSize, pageSize int) (items []*ScenicSpotView, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM scenic_spot WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY create_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type ScenicSpotPageResp struct {
	List   []*ScenicSpotView
	Paging *utils.PagingResp
}

func (r *ScenicSpot) GetScenicSpotAll(condition string, pars []interface{}) (items []*ScenicSpotView, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM scenic_spot WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY create_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

type ScenicSpotAllReq struct {
	ScenicSpotName string `description:"景区名称"`
}

type ScenicSpotListReq struct {
	ScenicSpotName string `description:"景区名称"`
	utils.PagingReq
}

type ScenicSpotAddReq struct {
	ScenicSpotName string `description:"景区名称"`
	Address        string `description:"地址"`
}

// 修改
func (obj *ScenicSpot) Update(updateParams, whereParam map[string]interface{}) (err error) {
	to := orm.NewOrm()
	ptrStructOrTableName := "scenic_spot"

	qs := to.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	return
}

type ScenicSpotEditReq struct {
	ScenicSpotId   int    `description:"景区id"`
	ScenicSpotName string `description:"景区名称"`
	Address        string `description:"地址"`
}

type ScenicSpotEnableOrDisableReq struct {
	ScenicSpotId int `description:"景区id"`
}
